package com.ply.service.impl;

import com.ply.entity.pojo.Project;
import com.ply.entity.pojo.VoteOption;
import com.ply.entity.vo.ProjectAndOption;
import com.ply.entity.vo.ProjectAndOptionSum;
import com.ply.entity.vo.Result;
import com.ply.mapper.LevelMapper;
import com.ply.mapper.ProjectMapper;
import com.ply.service.ProjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class ProjectServiceImpl implements ProjectService {

    @SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
    @Autowired
    ProjectMapper projectMapper;
    @SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
    @Autowired
    LevelMapper levelMapper;

    /**
     * 根据用户id，投票名字，每日投票次数,选项，开始时间，结束时间，单选多选创建投票
     * 返回创建是否成功
     * <p>
     * //     * @param userId  用户id
     */
    public Result<?> createVote(ProjectAndOption po) {

        //获取当前时间为创建时间(不需要数据库中有默认)
        //发送请求给客服审核,审核成功返回创建成功，审核失败返回创建失败，添加审核人ID数据，修改状态
        long startTime = po.getProject().getStartAt().getTime();
        if (startTime <= System.currentTimeMillis()) {
            return Result.fail("开始时间已经过了，创建失败");
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(po.getProject().getStartAt());
        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 5);//让日期加5
        Date endAt = calendar.getTime();
        int i = projectMapper.AddCreateVoteProject(po.getProject().getUserId(), po.getProject().getName(),
                po.getProject().getVoteNumEvery(), po.getProject().getStartAt(), endAt, po.getProject().getIsRadio());

        if (i == 0) {
            return Result.fail("数据库 add vote 失败");
        }

        int creatVoteId = projectMapper.selectMaxProjectId();
        for (VoteOption v : po.getVoteOptionList()) {
            int i1 = projectMapper.AddCreateVoteOption(creatVoteId, v.getName()
                    , v.getDetails());

            if (i1 == 0) {
                return Result.fail("选项表 加入失败");
            }


        }
        return Result.ok("创建投票成功");
    }


    /**
     * 根据用户id，投票名字，每日投票次数,选项，开始时间，结束时间，单选多选创建投票
     * 返回创建是否成功
     * <p>
     * //     * @param userId  用户id
     */
    public Result<?> createVote(Project po) {

        //获取当前时间为创建时间(不需要数据库中有默认)
        //发送请求给客服审核,审核成功返回创建成功，审核失败返回创建失败，添加审核人ID数据，修改状态
        long startTime = po.getStartAt().getTime();
        if (startTime <= System.currentTimeMillis()) {
            return Result.fail("开始时间已经过了，创建失败");
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(po.getStartAt());
        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 5);//让日期加5
        Date endAt = calendar.getTime();
        projectMapper.AddCreateVoteProject(po.getUserId(), po.getName(),
                po.getVoteNumEvery(), po.getStartAt(), endAt, po.getIsRadio());


        //加入 选项 到数据库
        return Result.ok("创建投票成功");
    }


    // 添加一个list选项
    public Result<?> addVoteOptionList(Integer projectId,List<VoteOption> voteOptionList) {
        for (VoteOption voteOption : voteOptionList) {
            addVoteOption(projectId,voteOption);
        }
        return Result.ok();
    }

    // 添加单个选项
    public Result<?> addVoteOption(Integer projectId,VoteOption voteOption) {
        if (projectMapper.selectProject(projectId) == null){
            return Result.fail();
        }
        projectMapper.AddCreateVoteOption(projectId,voteOption.getName(),voteOption.getDetails());
        return Result.ok();
    }

        /**
         * 根据ip给选项进行投票，返回是否成功
         */
    public Result<?> toVote(int optionId, String ip) {
        if (projectMapper.selectOptionis(optionId) == null) {
            return Result.fail("没有这个选项");
        }
        //成功 选项投票数 加1  失败false
        int projectId = projectMapper.selectProjectIdByOptionId(optionId);
        int status = projectMapper.selectStatusByProjectId(projectId);
        if (status != 3) {
            return Result.fail("该项目不能投票");
        }
        int num = projectMapper.selectVoteNumByIp(projectId, ip);
        int voteNumEvery = projectMapper.selectVoteNumEveryByProjectId(projectId);
        if (num >= voteNumEvery) {
            return Result.fail("今天投票数已达上限");
        }
        int i = projectMapper.addVoteNum(optionId);
        if (i == 0) {
            return Result.fail("投票失败");
        }
        int j = projectMapper.addVoteRecord(optionId, ip);
        if (j == 0) {
            return Result.fail("增加投票记录失败");
        }
        return Result.ok();
    }

    /**
     * 给项目延迟结束时间
     */
    public Result<?> byProjectEndTime(int userId, int projectId, int time) {
        if (time <= 0 || time > 100) {
            return Result.fail("时间不合理");
        }
        if (projectMapper.selectProject(projectId) == null) {
            return Result.fail("项目不存在");
        }
        Date endAt = projectMapper.selectEndAtByProject(projectId);
        long nowEndAt = endAt.getTime();
        if (nowEndAt <= System.currentTimeMillis()) {
            return Result.fail("项目已经结束，不能延长时间");
        }
        log.info("增加的时间,{}", time);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(endAt);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + time);
        Date newEndAt = calendar.getTime();
        int i = projectMapper.updateEndAt(userId, projectId, newEndAt);
        if (i == 0) {
            return Result.fail("延迟结束时间失败");
        }
        return Result.ok("延迟结束时间成功");
    }


    /**
     * 根据项目id查询项目信息
     */
    public ProjectAndOption selectProjectByProjectId(int projectId) {
        if (projectMapper.selectProject(projectId) == null) {
            return null;
        }
        ProjectAndOption po = new ProjectAndOption();
        po.setProject(projectMapper.selectProjectByProjectId(projectId));
        po.setVoteOptionList(projectMapper.selectOptionByProjectId(projectId));
        return po;
    }

    /**
     * 增加项目的审核人id
     */
    public Result<?> addProjectAuditId(int projectId, int auditId) {
        if (projectMapper.selectProject(projectId) == null) {
            return Result.fail("项目不存在");
        }
        int i = projectMapper.addProjectAuditId(projectId, auditId);
        if (i == 0) {
            return Result.fail("增加审核人id失败");
        }
        return Result.ok();
    }

    /**
     * 修改项目的状态
     */
    public Result<?> updateProjectStatus(int projectId, int status) {
        if (projectMapper.selectProject(projectId) == null) {
            return Result.fail("项目不存在");
        }
        if (status == 1) {
            return Result.fail("当前项目已经是未审核状态了，请修改为其他状态");
        }
        if (status <= 0 || status > 5) {
            return Result.fail("没有这个状态");
        }
        if (status == 3) {
            return Result.fail("不能改为这个状态，请修改为其他状态");
        }
        if (status == 4) {
            return Result.fail("不能改为这个状态，请修改为其他状态");
        }

        int i = projectMapper.updateProjectStatus(projectId, status);
        if (i == 0) {
            return Result.fail("修改项目状态失败");
        }

        return Result.ok(projectMapper.selectUserIdByProjectId(projectId));
    }

    /**
     * 根据项目id查询单个项目的项目名字，选项名字，投票数。
     *
     * @param projectId
     * @return
     */
    public Result selectProjectNameAndOptionNameAndSum(int projectId) {
        if (projectMapper.selectProject(projectId) == null) {
            return null;
        }
        ProjectAndOptionSum projectAndOptionSum = new ProjectAndOptionSum();
        projectAndOptionSum.setProjectName(projectMapper.selectProjectByProjectId(projectId).getName());
        projectAndOptionSum.setOptionAndSums(projectMapper.selectOptionNameAndSum(projectId));
        return Result.ok(projectAndOptionSum);
    }
}
